package com.spring.Log;

import com.spring.Entity.AdminEntity;
import com.spring.Entity.UserEntity;
import com.spring.Service.Interface.AdminService;
import com.spring.Service.Interface.LogService;
import com.spring.Service.Interface.UserServie;
import com.spring.Service.LogServiceImpl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * Created by CMCC-Sunmo on 2016/9/7.
 */
@Aspect
@Component
public class AdminLog {
    private static String AdminDomain = "管理员";

    @Autowired
    private LogService Log;
    @Autowired
    private AdminService adminService;

    @Before("execution(* com.spring.Controller.AdminControllerImpl.testLogin(..))")
    public void adminlogin(JoinPoint joinPoint) {
        Object[] values = joinPoint.getArgs();
        AdminEntity adminEntity = (AdminEntity) values[0];
        HttpServletRequest request = (HttpServletRequest) values[values.length - 1];
        Log.log(adminEntity.getAdminName(), adminEntity.getAdminID(), request.getRemoteAddr(), LocalDateTime.now(), AdminDomain, "登录操作");
    }

    @Before("execution(* com.spring.Controller.AdminControllerImpl.logOut(..))")
    public void adminlogout(JoinPoint joinPoint) {
        Object[] value = joinPoint.getArgs();
        HttpSession session = (HttpSession) value[0];
        HttpServletRequest request = (HttpServletRequest) value[value.length - 1];
        String adminId = (String) session.getAttribute("SID");
        if (!adminId.equals("null")) {
            AdminEntity adminEntity = adminService.findById(adminId);
            Log.log(adminEntity.getAdminName(), adminId, request.getRemoteAddr(), LocalDateTime.now(), AdminDomain, "登出操作");
        }
    }

    //管理员查看所有合同
    @Before("execution(* com.spring.Controller.ContractControllerImpl.findContractAll(..))")
    public void adminWatchContract(JoinPoint joinPoint){
        Object[] value = joinPoint.getArgs();
        HttpSession session = (HttpSession) value[0];
        HttpServletRequest request = (HttpServletRequest) value[value.length - 1];
        String adminId = (String) session.getAttribute("SID");
        if (!adminId.equals("null")) {
            AdminEntity adminEntity = adminService.findById(adminId);
            Log.log(adminEntity.getAdminName(), adminId, request.getRemoteAddr(), LocalDateTime.now(), AdminDomain, "查看所有合同");
        }
    }

    //管理员搜索所有合同
    @Before("execution(* com.spring.Controller.ContractControllerImpl.searchReqByAdmin(..))")
    public void adminSearchContract(JoinPoint joinPoint){
        Object[] value = joinPoint.getArgs();
        HttpSession session = (HttpSession) value[5];
        HttpServletRequest request = (HttpServletRequest) value[value.length - 1];
        String adminId = (String) session.getAttribute("SID");

        String ContractId = value[0].toString();
        String ContractName = value[1].toString();
        String UserName = value[2].toString();
        String Start = value[3].toString();
        String Deadline = value[4].toString();

        String searchCon = "搜索条件："+"ContractID("+ContractId+"),ContractName("+ContractName+"),UserName("+UserName+"),Start("+Start+"),Deadline("+Deadline+")";

        if (!adminId.equals("null")) {
            AdminEntity adminEntity = adminService.findById(adminId);
            Log.log(adminEntity.getAdminName(), adminId, request.getRemoteAddr(), LocalDateTime.now(), AdminDomain, "搜索合同,"+searchCon);
        }
    }


    //管理员查看所有用户
    @Before("execution(* com.spring.Controller.UserControllerImpl.findUsersAll(..))")
    public void adminWatchUser(JoinPoint joinPoint){
        Object[] value = joinPoint.getArgs();
        HttpSession session = (HttpSession) value[0];
        HttpServletRequest request = (HttpServletRequest) value[value.length - 1];
        String adminId = (String) session.getAttribute("SID");
        if (!adminId.equals("null")) {
            AdminEntity adminEntity = adminService.findById(adminId);
            Log.log(adminEntity.getAdminName(), adminId, request.getRemoteAddr(), LocalDateTime.now(), AdminDomain, "查看所有用户");
        }
    }

    //管理员搜索所有用户
    @Before("execution(* com.spring.Controller.UserControllerImpl.findReqUsersAll(..))")
    public void adminSearchUser(JoinPoint joinPoint){
        Object[] value = joinPoint.getArgs();
        HttpSession session = (HttpSession) value[1];
        HttpServletRequest request = (HttpServletRequest) value[value.length - 1];
        String adminId = (String) session.getAttribute("SID");

        UserEntity userEntity = (UserEntity) value[0];
        String UserName = userEntity.getUserName();
        String Name = userEntity.getName();
        String Dept = userEntity.getDepartment();


        String searchCon = "搜索条件："+"UserName("+UserName+"),Name("+Name+"),Dept("+Dept+")";

        if (!adminId.equals("null")) {
            AdminEntity adminEntity = adminService.findById(adminId);
            Log.log(adminEntity.getAdminName(), adminId, request.getRemoteAddr(), LocalDateTime.now(), AdminDomain, "搜索合同,"+searchCon);
        }
    }

    //管理员通过Excel导入用户
    @Before("execution(* com.spring.Controller.UserControllerImpl.addUserExcel(..))")
    public void adminAddUserByExcel(JoinPoint joinPoint){
        Object[] value = joinPoint.getArgs();
        HttpSession session = (HttpSession) value[1];
        HttpServletRequest request = (HttpServletRequest) value[value.length - 1];
        String adminId = (String) session.getAttribute("SID");

        if (!adminId.equals("null")) {
            AdminEntity adminEntity = adminService.findById(adminId);
            Log.log(adminEntity.getAdminName(), adminId, request.getRemoteAddr(), LocalDateTime.now(), AdminDomain, "通过Excel添加用户信息");
        }
    }

    //管理员修改用户密码
    @AfterReturning("execution(* com.spring.Controller.UserControllerImpl.changePassByAdmin(..))")
    public void adminChangeUserPass(JoinPoint joinPoint){
        Object[] value = joinPoint.getArgs();
        HttpSession session = (HttpSession) value[2];
        HttpServletRequest request = (HttpServletRequest) value[value.length - 1];
        String adminId = (String) session.getAttribute("SID");

        String userID = value[0].toString();

        if (!adminId.equals("null")) {
            AdminEntity adminEntity = adminService.findById(adminId);
            Log.log(adminEntity.getAdminName(), adminId, request.getRemoteAddr(), LocalDateTime.now(), AdminDomain, "修改用户密码,用户ID："+userID);
        }
    }

    //管理员修改用户信息
    @AfterReturning("execution(* com.spring.Controller.UserControllerImpl.updateUserByAdmin(..))")
    public void adminChangeUserInfo(JoinPoint joinPoint){
        Object[] value = joinPoint.getArgs();
        HttpSession session = (HttpSession) value[1];
        HttpServletRequest request = (HttpServletRequest) value[value.length - 1];
        String adminId = (String) session.getAttribute("SID");

        UserEntity userEntity = (UserEntity) value[0];
        String userID = userEntity.getId();

        if (!adminId.equals("null")) {
            AdminEntity adminEntity = adminService.findById(adminId);
            Log.log(adminEntity.getAdminName(), adminId, request.getRemoteAddr(), LocalDateTime.now(), AdminDomain, "修改用户信息,用户ID："+userID);
        }
    }


    //管理员删除用户
    @AfterReturning("execution(* com.spring.Controller.UserControllerImpl.deleteByUserId(..))")
    public void adminDeleteUser(JoinPoint joinPoint){
        Object[] value = joinPoint.getArgs();
        HttpSession session = (HttpSession) value[1];
        HttpServletRequest request = (HttpServletRequest) value[value.length - 1];
        String adminId = (String) session.getAttribute("SID");

        UserEntity userEntity = (UserEntity) value[0];
        String userID = userEntity.getId();

        if (!adminId.equals("null")) {
            AdminEntity adminEntity = adminService.findById(adminId);
            Log.log(adminEntity.getAdminName(), adminId, request.getRemoteAddr(), LocalDateTime.now(), AdminDomain, "删除用户,用户ID："+userID);
        }
    }


    //管理员查看所有日志
    @Before("execution(* com.spring.Controller.LogControllerImpl.findLogsAll(..))")
    public void adminWatchLog(JoinPoint joinPoint){
        Object[] value = joinPoint.getArgs();
        HttpSession session = (HttpSession) value[0];
        HttpServletRequest request = (HttpServletRequest) value[value.length - 1];
        String adminId = (String) session.getAttribute("SID");
        if (!adminId.equals("null")) {
            AdminEntity adminEntity = adminService.findById(adminId);
            Log.log(adminEntity.getAdminName(), adminId, request.getRemoteAddr(), LocalDateTime.now(), AdminDomain, "查看所有日志");
        }
    }

    //管理员搜索所有日志
    @Before("execution(* com.spring.Controller.LogControllerImpl.findReqLogsAll(..))")
    public void adminSearchLog(JoinPoint joinPoint){
        Object[] value = joinPoint.getArgs();
        HttpSession session = (HttpSession) value[5];
        HttpServletRequest request = (HttpServletRequest) value[value.length - 1];
        String adminId = (String) session.getAttribute("SID");

        String UserName = value[0].toString();
        String IP = value[1].toString();
        String Oper = value[2].toString();
        String Start = value[3].toString();
        String End = value[4].toString();

        String searchCon = "搜索条件："+"UserName("+UserName+"),IP("+IP+"),Oper("+Oper+"),Start("+Start+"),End("+End+")";

        if (!adminId.equals("null")) {
            AdminEntity adminEntity = adminService.findById(adminId);
            Log.log(adminEntity.getAdminName(), adminId, request.getRemoteAddr(), LocalDateTime.now(), AdminDomain, "搜索合同,"+searchCon);
        }
    }
}
